package hbec.kettle.etl.plugins.core;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import hbec.kettle.etl.engine.conf.TransEntityConf;

public class Topology {
	private Map<String, TransEntityConf> vertexMap = new HashMap<>();
	private Map<TransEntityConf, Set<TransEntityConf>> adjacencys = new HashMap<>();
	
	private Set<TransEntityConf> header = null;
	
	public Topology (Set<TransEntityConf> vertexSet) {
		for (TransEntityConf tpc : vertexSet) 
			vertexMap.put(tpc.getTransName(), tpc);
		
		for (Map.Entry<String, TransEntityConf> e : vertexMap.entrySet()) {
			String depend = e.getValue().getTransDepend();
			if (depend == null || depend.isEmpty()) 
				header.add(e.getValue());
			else {
				add(vertexMap.get(depend), e.getValue());
			}
		}
	}
	
	public int getVertexCount() {
		return this.vertexMap.size();
	}
	
	public Set<TransEntityConf> getNextVertex(TransEntityConf v) {
		if (v == null) 
			return header;
		else 
			return adjacencys.get(v);
	}
	
	private void add(TransEntityConf v1, TransEntityConf v2) {
		Set<TransEntityConf> vs = null;
		if (adjacencys.containsKey(v1)) 
			vs = adjacencys.get(v1);
		else
			vs = new HashSet<>();
		
		vs.add(v2);
		adjacencys.put(v1, vs);
	}
}
